<template>
  <div class="organizer-intro">
    <div
      class="page-header image"
      :style="bannerImage ? { backgroundImage: `url(${bannerImage})` } : {}"
    >
      <div class="header-overlay">
        <div class="container">
          <h1 class="page-title">{{ "主办方介绍" }}</h1>
          <p class="page-subtitle">
            {{ pageData.subtitle || "了解展会主办方的专业背景和服务能力" }}
          </p>
        </div>
      </div>
    </div>
    <!-- 导航菜单 -->
    <div class="nav-menu">
      <div class="container">
        <nav class="about-nav">
          <a
            @click="setActiveTab('exhibition-intro')"
            :class="['nav-item', { active: activeTab === 'exhibition-intro' }]"
          >
            展会介绍
          </a>
          <a
            @click="setActiveTab('product-scope')"
            :class="['nav-item', { active: activeTab === 'product-scope' }]"
          >
            展品范围
          </a>
          <a
            @click="setActiveTab('exhibition-highlights')"
            :class="[
              'nav-item',
              { active: activeTab === 'exhibition-highlights' },
            ]"
          >
            展会集锦
          </a>
          <a
            @click="setActiveTab('venue-intro')"
            :class="['nav-item', { active: activeTab === 'venue-intro' }]"
          >
            展馆介绍
          </a>
          <a
            @click="setActiveTab('organizer-intro')"
            :class="['nav-item', { active: activeTab === 'organizer-intro' }]"
          >
            主办方介绍
          </a>
          <a
            @click="setActiveTab('organization-structure')"
            :class="[
              'nav-item',
              { active: activeTab === 'organization-structure' },
            ]"
          >
            组织架构
          </a>
          <a
            @click="setActiveTab('past-exhibitions')"
            :class="['nav-item', { active: activeTab === 'past-exhibitions' }]"
          >
            往届展会
          </a>
          <a
            @click="setActiveTab('downloads')"
            :class="['nav-item', { active: activeTab === 'downloads' }]"
          >
            下载中心
          </a>
        </nav>
      </div>
    </div>

    <div class="container">
      <!-- 单条数据显示 -->
      <section class="intro-section" v-if="!organizerList.length">
        <div class="intro-content">
          <div v-if="pageData.imageUrl" class="intro-image">
            <img
              :src="pageData.imageUrl"
              :alt="pageData.title || '主办方介绍'"
            />
          </div>
          <div
            class="intro-description"
            v-html="pageData.content || defaultContent"
          ></div>
        </div>
      </section>

      <!-- 多条数据显示：上下结构，图片在上，内容在下 -->
      <section v-if="organizerList.length" class="organizer-list">
        <div
          class="organizer-card"
          v-for="organizer in organizerList"
          :key="organizer.id"
        >
          <div class="organizer-image" v-if="organizer.image">
            <img :src="organizer.image" :alt="organizer.title" />
          </div>
          <div class="organizer-body">
            <!-- <h3 class="organizer-title">{{ organizer.title }}</h3> -->
            <div
              class="organizer-description"
              v-if="organizer.description"
              v-html="organizer.description"
            ></div>
          </div>
        </div>
      </section>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from "vue";
import { subpageApi } from "@/api/user";

export default {
  name: "OrganizerIntro",
  setup() {
    const activeTab = ref("organizer-intro");
    const pageData = ref({});
    const organizerStats = ref({});
    const organizerList = ref([]);
    const bannerImage = ref("");

    // 默认内容
    const defaultContent = `
      <p>华南工业博览会组委会是专业的展会组织机构，拥有丰富的展会策划和执行经验。我们致力于为参展商和观众提供最优质的展会服务，推动机器人产业的健康发展。</p>
    `;

    // 默认统计数据
    const defaultStats = {
      experience: "15+",
      partners: "500+",
      visitors: "50,000+",
    };

    // 解析content字段（兼容JSON字符串或纯文本）
    const parseContent = (content) => {
      if (!content) return { content: "" };
      try {
        if (typeof content === "string" && content.trim().startsWith("{")) {
          const parsed = JSON.parse(content);
          return {
            content: parsed.content || "",
            subtitle: parsed.subtitle || "",
            backgroundImage: parsed.backgroundImage || "",
            videoUrl: parsed.videoUrl || "",
            isFeatured: parsed.isFeatured || false,
          };
        }
        return { content };
      } catch (e) {
        console.log("解析content失败:", e);
        return { content };
      }
    };

    // 加载页面数据
    const loadPageData = async () => {
      try {
        // 先尝试加载多条数据列表
        const listRes = await subpageApi.getSubpageList(
          "about",
          "organizer-intro"
        );
        if (
          listRes &&
          listRes.code === "200" &&
          Array.isArray(listRes.data) &&
          listRes.data.length > 0
        ) {
          organizerList.value = listRes.data.map((item, index) => {
            let parsed = {};
            try {
              parsed = item.content
                ? typeof item.content === "string"
                  ? JSON.parse(item.content)
                  : item.content
                : {};
            } catch (e) {
              console.log("解析content失败:", e);
            }

            return {
              id: item.id || index + 1,
              title: item.title || `主办方${index + 1}`,
              description: parsed.content || item.content || "",
              image: item.imageUrl || "",
            };
          });

          // 设置页面基本信息
          pageData.value = {
            title: "主办方介绍",
            subtitle: "了解展会主办方的专业背景和服务能力",
          };
          return;
        }

        // 如果没有多条数据，加载单条数据
        const response = await subpageApi.getSubpageByKey(
          "about",
          "organizer-intro"
        );
        if (response && response.code === "200" && response.data) {
          const data = response.data;
          const parsed = parseContent(data.content);
          pageData.value = {
            title: data.title || "主办方介绍",
            subtitle:
              data.subtitle ||
              parsed.subtitle ||
              "了解展会主办方的专业背景和服务能力",
            content: parsed.content || data.content || defaultContent,
            imageUrl: parsed.backgroundImage || data.imageUrl || "",
          };
          organizerStats.value = defaultStats;
        } else {
          // 使用默认数据
          pageData.value = {
            title: "主办方介绍",
            subtitle: "了解展会主办方的专业背景和服务能力",
            content: defaultContent,
            imageUrl: "",
          };
          organizerStats.value = defaultStats;
        }
      } catch (error) {
        console.error("加载主办方介绍数据失败:", error);
        pageData.value = {
          title: "主办方介绍",
          subtitle: "了解展会主办方的专业背景和服务能力",
          content: defaultContent,
          imageUrl: "",
        };
        organizerStats.value = defaultStats;
      }
    };

    const loadBanner = async () => {
      try {
        const resp = await subpageApi.getContentByType("carousel_about");
        if (
          resp &&
          resp.code === "200" &&
          Array.isArray(resp.data) &&
          resp.data.length
        ) {
          const first =
            resp.data.find((it) => it.status === "active") || resp.data[0];
          const content =
            typeof first.content === "string"
              ? JSON.parse(first.content || "{}")
              : first.content || {};
          bannerImage.value = first.imageUrl || content.image || "";
        }
      } catch (e) {
        bannerImage.value = "";
      }
    };

    const setActiveTab = (tabKey) => {
      if (!tabKey) return;
      activeTab.value = tabKey;
      // 跳转到对应的子页面
      if (tabKey !== "organizer-intro") {
        window.location.href = `/${tabKey}`;
      }
    };

    onMounted(() => {
      loadPageData();
      loadBanner();
    });

    return {
      activeTab,
      pageData,
      organizerStats,
      bannerImage,
      organizerList,
      defaultContent,
      setActiveTab,
    };
  },
};
</script>

<style scoped>
.organizer-intro {
  min-height: 100vh;
  background: #f8f9fa;
  /* padding-top: 70px; */
}
:deep(.resizable-image) {
  width: 100% !important;
  max-width: 100% !important;
  height: auto !important;
  margin: 0 !important;
}

.page-header {
  background: linear-gradient(135deg, #320063 0%, #4a0a8a 100%);
  color: white;
  padding: 80px 0;
  text-align: center;
}

.page-header.image {
  position: relative;
  background: none;
  padding: 0;
  height: 400px;
  background-size: cover;
  background-position: center;
}
.header-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  display: flex;
  align-items: center;
}

.page-title {
  font-size: 48px;
  margin-bottom: 20px;
}

.page-subtitle {
  font-size: 18px;
  opacity: 0.9;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.organizer-section {
  background: white;
  border-radius: 20px;
  padding: 3rem;
  margin: 2rem 0;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
}

.section-title {
  font-size: 2rem;
  color: #333;
  margin-bottom: 2rem;
  text-align: center;
}

.organizer-content {
  max-width: 800px;
  margin: 0 auto;
}

.organizer-info h3 {
  font-size: 1.8rem;
  color: #320063;
  margin-bottom: 1.5rem;
  font-weight: 600;
}

.organizer-info p {
  color: #666;
  line-height: 1.8;
  font-size: 1.1rem;
  margin-bottom: 2rem;
}

/* 富文本样式增强 */
.organizer-richtext {
  color: #555;
  line-height: 1.8;
  font-size: 1.05rem;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.organizer-richtext :deep(p) {
  margin: 0 0 12px;
}
.organizer-richtext :deep(ul),
.organizer-richtext :deep(ol) {
  margin: 8px 0 12px;
  padding-left: 22px;
}
.organizer-richtext :deep(li) {
  margin: 6px 0;
  line-height: 1.7;
}
.organizer-richtext :deep(h1),
.organizer-richtext :deep(h2),
.organizer-richtext :deep(h3),
.organizer-richtext :deep(h4),
.organizer-richtext :deep(h5),
.organizer-richtext :deep(h6) {
  margin: 14px 0 8px;
  color: #333;
  font-weight: 600;
}
.organizer-richtext :deep(img) {
  max-width: 100%;
  height: auto;
  border-radius: 8px;
  display: block;
  margin: 10px 0;
}

/* 去除统计卡片样式（已移除DOM） */

@media (max-width: 768px) {
  .organizer-section {
    padding: 2rem;
    margin: 1rem 0;
  }

  .section-title {
    font-size: 1.5rem;
  }

  .organizer-stats {
    grid-template-columns: 1fr;
    gap: 1.5rem;
  }

  .page-title {
    font-size: 2rem;
  }

  .page-subtitle {
    font-size: 1rem;
  }
}

/* 导航菜单样式 - 参考关于我们页面 */
.nav-menu {
  background: #f8f9fa;
  padding: 20px 0;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.about-nav {
  display: flex;
  justify-content: center;
  gap: 30px;
  flex-wrap: wrap;
}

.nav-item {
  text-decoration: none;
  color: #333;
  font-size: 1.1rem;
  font-weight: 500;
  padding: 10px 20px;
  border-radius: 8px;
  transition: background-color 0.3s ease, color 0.3s ease;
  border: 1px solid #eee;
  cursor: pointer;
}

.nav-item:hover {
  background-color: var(--e-global-color-selected);
  color: white;
  border-color: var(--e-global-color-selected);
}

.nav-item.active {
  background-color: var(--e-global-color-selected);
  color: white;
  border-color: var(--e-global-color-selected);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .about-nav {
    gap: 15px;
  }

  .nav-item {
    font-size: 1rem;
    padding: 8px 16px;
  }
}

/* 多条数据布局样式 - 上下结构 */
.organizer-list {
  margin-top: 2rem;
}

.organizer-card {
  background: white;
  border-radius: 15px;
  overflow: hidden;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  margin-bottom: 2rem;
}

.organizer-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.15);
}

.organizer-image {
  width: 100%;
  height: 300px;
  overflow: hidden;
}

.organizer-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.organizer-card:hover .organizer-image img {
  transform: scale(1.05);
}

.organizer-body {
  padding: 2rem;
}

.organizer-title {
  font-size: 1.5rem;
  font-weight: 600;
  color: #333;
  margin-bottom: 1rem;
  text-align: center;
}

.organizer-description {
  color: #666;
  line-height: 1.8;
  font-size: 1rem;
}

.organizer-description :deep(h1),
.organizer-description :deep(h2),
.organizer-description :deep(h3),
.organizer-description :deep(h4) {
  color: #333;
  margin: 1.5rem 0 1rem 0;
  font-weight: 600;
}

.organizer-description :deep(p) {
  margin-bottom: 1rem;
  text-align: justify;
}

.organizer-description :deep(ul),
.organizer-description :deep(ol) {
  margin: 1rem 0;
  padding-left: 2rem;
}

.organizer-description :deep(li) {
  margin-bottom: 0.5rem;
}

.organizer-description :deep(img) {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 1.5rem auto;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

/* 单条数据显示样式 */
.intro-section {
  margin-top: 2rem;
}

.intro-content {
  background: white;
  border-radius: 15px;
  padding: 2rem;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.intro-image {
  text-align: center;
  margin-bottom: 2rem;
}

.intro-image img {
  max-width: 100%;
  height: auto;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
}

.intro-description {
  color: #666;
  line-height: 1.8;
  font-size: 1rem;
}

.intro-description :deep(h1),
.intro-description :deep(h2),
.intro-description :deep(h3),
.intro-description :deep(h4) {
  color: #333;
  margin: 1.5rem 0 1rem 0;
  font-weight: 600;
}

.intro-description :deep(p) {
  margin-bottom: 1rem;
  text-align: justify;
}

/* 响应式样式 */
@media (max-width: 768px) {
  .organizer-card {
    margin-bottom: 1.5rem;
  }

  .organizer-image {
    height: 200px;
  }

  .organizer-body {
    padding: 1.5rem;
  }

  .organizer-title {
    font-size: 1.3rem;
  }

  .intro-content {
    padding: 1.5rem;
  }
}
</style>
